package com.kpf.springcloud.service.impl;

import com.kpf.springcloud.common.ResponseEnum;
import com.kpf.springcloud.exception.BusinessException;
import com.kpf.springcloud.mapper.TopicLikeMapper;
import com.kpf.springcloud.pojo.KpfTopic;
import com.kpf.springcloud.pojo.KpfTopicLike;
import com.kpf.springcloud.service.IKpfTopicLikeService;
import com.kpf.springcloud.service.IKpfTopicService;
import com.kpf.springcloud.service.ITokenService;
import com.kpf.springcloud.utils.GeneralUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shiwen
 * @date 2021/3/7
 */
@Service
public class KpfTopicLikeServiceImpl extends BaseServiceImpl<TopicLikeMapper, KpfTopicLike> implements IKpfTopicLikeService {

    @Autowired
    private ITokenService tokenService;

    @Autowired
    private IKpfTopicService topicService;

    /**
     * 点赞接口
     *
     * @param topicId
     * @param status 点赞状态 0：未点赞 1：已点赞
     */
    @Override
    public void likeTopic(int topicId, int status) {
        // 获取token里的userId
        String token = tokenService.verifyToken();
        if ("TOKEN_EXPIRED".equals(token)) {
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        int userId = Integer.parseInt(token.split("-")[0]);
        List<KpfTopicLike> kpfTopicLikeList = list(getInitQueryWrapper().eq(KpfTopicLike::getUserId, userId).eq(KpfTopicLike::getTopicId, topicId));
        if (status == 1) {
            if (GeneralUtil.isListNull(kpfTopicLikeList)) {
                // 如果没有这个集合的数据则说明没有点赞过
                KpfTopicLike kpfTopicLike = KpfTopicLike.builder().likeStatus(status).userId(userId).topicId(topicId).build();
                save(kpfTopicLike);
                // 更新当前帖子的点赞人数 + 1
                KpfTopic kpfTopic = topicService.getById(topicId);
                if (GeneralUtil.isObjNotNull(kpfTopic)) {
                    kpfTopic.setLikeNum(kpfTopic.getLikeNum() + 1);
                    topicService.update(kpfTopic, topicService.getLambdaUpdateWrapper().eq(KpfTopic::getId, kpfTopic.getId()));
                }
            } else {
                // 如果已存在, 并且为未点赞, 则直接更新为点赞
                if (kpfTopicLikeList.get(0).getLikeStatus() == 0) {
                    KpfTopicLike topicLike = kpfTopicLikeList.get(0);
                    topicLike.setLikeStatus(1);
                    update(topicLike, getLambdaUpdateWrapper().eq(KpfTopicLike::getId, topicLike.getId()));
                    // 更新当前帖子的点赞人数 + 1
                    KpfTopic kpfTopic = topicService.getById(topicId);
                    if (GeneralUtil.isObjNotNull(kpfTopic)) {
                        kpfTopic.setLikeNum(kpfTopic.getLikeNum() + 1);
                        topicService.update(kpfTopic, topicService.getLambdaUpdateWrapper().eq(KpfTopic::getId, kpfTopic.getId()));
                    }
                }

            }
        } else {
            // 取消点赞的逻辑 => 数据不为空并且已点赞
            if (GeneralUtil.isListNotNull(kpfTopicLikeList) && kpfTopicLikeList.get(0).getLikeStatus() == 1) {
                KpfTopicLike kpfTopicLike = kpfTopicLikeList.get(0);
                kpfTopicLike.setLikeStatus(0);
                update(kpfTopicLike, getLambdaUpdateWrapper().eq(KpfTopicLike::getId, kpfTopicLike.getId()));
                // 更新当前帖子的点赞人数 - 1
                KpfTopic kpfTopic = topicService.getById(topicId);
                if (GeneralUtil.isObjNotNull(kpfTopic)) {
                    kpfTopic.setLikeNum(kpfTopic.getLikeNum() - 1);
                    topicService.update(kpfTopic, topicService.getLambdaUpdateWrapper().eq(KpfTopic::getId, kpfTopic.getId()));
                }
            }
        }
    }


}